﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using ImageAppViewModels;
using Microsoft.Win32;
using BrainTechLLC.FilterExtensions;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FilterCollectionCollectionView.xaml
    /// </summary>
    public partial class FilterCollectionCollectionView : UserControl
    {
        public static readonly DependencyProperty FilterCollectionCollectionViewModelProperty =
            DependencyProperty.Register("FilterCollectionCollectionViewModel", typeof(FilterCollectionCollectionViewModel), typeof(FilterCollectionCollectionView), new PropertyMetadata(null, OnFilterCollectionCollectionViewModelPropertyChanged));

        public FilterCollectionCollectionViewModel FilterCollectionCollectionViewModel
        {
            get { return (FilterCollectionCollectionViewModel)GetValue(FilterCollectionCollectionViewModelProperty); }
            set { SetValue(FilterCollectionCollectionViewModelProperty, value); }
        }

        private static void OnFilterCollectionCollectionViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FilterCollectionCollectionView source = d as FilterCollectionCollectionView;
            source.UpdateFilterCollectionCollectionViewModel(e.NewValue as FilterCollectionCollectionViewModel);
        }

        private void UpdateFilterCollectionCollectionViewModel(FilterCollectionCollectionViewModel collections)
        {
            FilterCollectionCollectionViewModel = collections;

            // Let the UI bind to the view-model.
            this.DataContext = FilterCollectionCollectionViewModel;
            grid.DataContext = FilterCollectionCollectionViewModel;
            FilterCollectionCollectionViewModel.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(FilterCollectionCollectionViewModel_CollectionChanged);
            FilterCollectionCollectionViewModel.FilterCollectionsChanged += new EventHandler<System.Collections.Specialized.NotifyCollectionChangedEventArgs>(FilterCollectionCollectionViewModel_FilterCollectionsChanged);
        }

        void FilterCollectionCollectionViewModel_FilterCollectionsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            TryRebind();
        }

        public void TryRebind()
        {
            this.cboUserDefined.InvalidateVisual();
            this.cboUserDefined.ItemsSource = FilterCollectionCollectionViewModel.FilterCollectionNames;
        }

        void FilterCollectionCollectionViewModel_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            TryRebind();
        }

        public FilterCollectionCollectionView()
        {
            InitializeComponent();
        }

        public void UseSelectedFilterCollection()
        {
            string newSelection = cboUserDefined.SelectedItem as string;

            if (newSelection != null)
            {
                if (newSelection == "Blur Filters 1")
                {
                    Window1.MainWindow.FilterHandler.CurrentFilterCollection = Window1.MainWindow.FilterHandler._blurFilters1;
                }
                else if (newSelection == "Blur Filters 2")
                {
                    Window1.MainWindow.FilterHandler.CurrentFilterCollection = Window1.MainWindow.FilterHandler._blurFilters2;
                }
                else if (newSelection == "Comparison Filters")
                {
                    Window1.MainWindow.FilterHandler.CurrentFilterCollection = Window1.MainWindow.FilterHandler._differenceFilters1;
                }
                else
                {
                    var found = FilterCollectionCollectionViewModel.FindByName(newSelection);

                    if (found != null)
                    {
                        Window1.MainWindow.FilterHandler.CurrentFilterCollection = found;
                    }
                }
            }
        }

        private void cboUserDefined_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UseSelectedFilterCollection();
        }

        private void Add(object sender, ExecutedRoutedEventArgs e)
        {
            string newName = txtAddFilterCollection.Text;

            if (string.IsNullOrEmpty(newName))
                return;

            GeneralCommands.FilterCollectionAddCommand.Execute(FilterCollectionCollectionViewModel, newName);
        }

        private void Rename(object sender, ExecutedRoutedEventArgs e)
        {
            string selectedName = cboUserDefined.SelectedItem as string;

            if (selectedName != null)
            {
                string newName = txtAddFilterCollection.Text;

                if (!string.IsNullOrEmpty(newName))
                {
                    GeneralCommands.FilterCollectionRenameCommand.Execute(FilterCollectionCollectionViewModel, selectedName, newName);
                    cboUserDefined.SelectedItem = newName;
                    Settings.Values.CurrentFilterCollectionName = newName;
                }
            }
        }

        private void Remove(object sender, ExecutedRoutedEventArgs e)
        {
            string selectedName = cboUserDefined.SelectedItem as string;

            if (selectedName != null)
            {
                var result = MessageBox.Show("Are you sure?", "Are you sure?", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.Yes)
                {
                    GeneralCommands.FilterCollectionRemoveCommand.Execute(FilterCollectionCollectionViewModel, selectedName);
                }
            }
        }

        private void MakeCopy(object sender, ExecutedRoutedEventArgs e)
        {
            string selectedName = cboUserDefined.SelectedItem as string;

            if (selectedName != null)
            {
                GeneralCommands.FilterCollectionMakeCopyCommand.Execute(FilterCollectionCollectionViewModel, selectedName);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog()
            {
                RestoreDirectory = true,
            };
            bool? result = sfd.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                Window1.MainWindow.FilterHandler.CurrentFilterCollection.Save(sfd.FileName);
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog()
            {
                RestoreDirectory = true,
            };
            bool? result = ofd.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                FilterCollection fc = FilterCollection.Load(ofd.FileName);
                Window1.MainWindow.FilterHandler._filters.AddFilterCollection(fc);
            }
        }
    }
}
